package design.onekey.config;

import design.onekey.constant.Constants;
import design.onekey.model.SysFunction;
import design.onekey.service.ISysFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.text.MessageFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ChainDefinitionSectionMetaSource
 * @Description: 动态获取权限资源
 * @author blcloak
 * @date 2020-06-08 11:38:45
*/
public class ChainDefinitionSectionMetaSource{

	/**
	 * @Fields iSysFunctionService : 功能service注入
	 * @author blcloak
	 * @date 2020-06-08 08:43:13
	 */
	@Autowired
	@Lazy
	private ISysFunctionService iSysFunctionService;


	public Map<String, String> getObject() {
		Map<String, String> section = new LinkedHashMap<>();
		List<SysFunction> list;
		list = this.iSysFunctionService.getFunctionsByType(1);


		if (list != null && list.size() > 0) {
			for (SysFunction sysFunction : list) {
				if (1==sysFunction.getRule()) {
					//公开访问，不需要鉴权
					section.put(sysFunction.getFunctionUrl(), Constants.PUBLIC_STRING);
				}else if (2 == sysFunction.getRule()) {
					//只需校验登录token
					section.put(sysFunction.getFunctionUrl(), Constants.JWT_STRING);
				}else { //需校验登录token和访问权限
					section.put(sysFunction.getFunctionUrl(),
								MessageFormat.format(Constants.PC_ADMIN_PREMISSION_STRING, sysFunction.getFunctionCode()));
				}
			}
		}
        return section;
	}

}
